package com.chijun.mform.field.impl;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chijun.mform.FormBuilder;
import com.chijun.mform.R;
import com.chijun.mform.create.dialog.IGetServerPersonResult;
import com.chijun.mform.create.dialog.ISelectProcessPerson;
import com.chijun.mform.create.dialog.SelectPersonNewDialog;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.field.BaseField;
import com.chijun.mform.process.dapater.ShowMutileOptionAdapter;
import com.example.ccj.b.c.custom.tree.TreeMode;
import com.example.ccj.b.c.recycler.BaseRecycler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.ObservableEmitter;

/**
 * 联系人
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/23 15:44
 */
public class FieldFor15LianXiRen extends BaseField {
    public FieldFor15LianXiRen(){
        super();
    }
    SelectPersonNewDialog selectPersonNewDialog;


    @Override
    public View getWritelView(Context context, WriteFormClass writeFormClass) {
        FormField formField = writeFormClass.formField;
        //提示文本
        if (formField.getHint() != null) writeFormClass.fieldFullContentView.setHint(formField.getHint());
        //输入文本颜色
        if (formField.getTextColor() != 0) writeFormClass.fieldFullContentView.setTextColor(formField.getTextColor());
        //是否只读
        if (formField.isReadOnly()) {
            writeFormClass.fieldFullContentView.setKeyListener(null);
            writeFormClass.fieldFullContentView.setFocusable(false);
        }

        writeFormClass.fieldFullContentView.setKeyListener(null);
        writeFormClass.fieldFullContentView.setFocusable(false);
        writeFormClass.fieldFullJumpImgView.setVisibility(View.VISIBLE);
        writeFormClass.fieldFullJumpImgView.setImageResource(R.drawable.shen_pi_add_process);


        writeFormClass.fieldFullJumpImgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectPersonNewDialog == null) {
                    selectPersonNewDialog = new SelectPersonNewDialog(context, "选择联系人",new ArrayList<>(),false, new ISelectProcessPerson() {
                        @Override
                        public void onOver(List<TreeMode> treeModeList) {
                            StringBuilder stringBuilder = new StringBuilder();
                            StringBuilder showStr = new StringBuilder();
                            for (TreeMode treeMode : treeModeList) {
                                stringBuilder.append(treeMode.dataId).append(",");
                                showStr.append(treeMode.name).append(",");
                            }
                            writeFormClass.fieldFullContentView.setText(showStr.toString());
                            saveValue = stringBuilder.toString();
                        }
                    });
                }
                selectPersonNewDialog.show();
            }
        });

        return writeFormClass.view;
    }


    @Override
    protected View getShowFullView(Context context, FullClass fullModel) {
        //字段名属性设置
        fullModel.setFieldProperty();
        //隐藏字段值
        fullModel.goneContentView();


        //放入选项展示
        BaseRecycler<TreeMode> recyclerView = new BaseRecycler<>(context);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL,false));

        List<String> selectedPerson = new ArrayList<>();
        String fieldValue = fullModel.getFieldValue();
        StringBuilder stringBuilder = new StringBuilder();
        List<TreeMode> treeModeLists  = new ArrayList<>();
        //已选择人员加载
        if (fieldValue != null) {
            stringBuilder.append(fieldValue);
            String[] split = fieldValue.split(",");
            selectedPerson.addAll(Arrays.asList(split));

            FormBuilder.getInstance().resultTreeModeList(new IGetServerPersonResult() {
                @Override
                public void result(List<TreeMode> treeModeList) {
                    Map<String,TreeMode> allTreeModeMap = new HashMap<>();
                    for (TreeMode treeMode : treeModeList) {
                        allTreeModeMap.put(treeMode.dataId,treeMode);
                    }
                    recyclerView.setAdapter(treeModeLists,new AdapterForShowLianXiRen(treeModeLists));
                    for (String s : split) {
                        TreeMode treeMode = allTreeModeMap.get(s);
                        if (treeMode != null) {
                            recyclerView.addDataOnTail(treeMode);
                        }
                    }
                }
            });

        } else {
            recyclerView.setAdapter(treeModeLists,new AdapterForShowLianXiRen(treeModeLists));
        }

        if (fullModel.isHaveModifyPower()) {
            fullModel.setJumpImageProperty(R.drawable.shen_pi_add_process, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SelectPersonNewDialog dialog = new SelectPersonNewDialog(fullModel.getContext(),
                            "选择联系人",
                            selectedPerson,
                            fullModel.formField.getLittleNum(),
                            false,
                            false, new ISelectProcessPerson() {
                        @Override
                        public void onOver(List<TreeMode> treeModeList) {
                            recyclerView.addDatas(treeModeList);
                            for (TreeMode treeMode : treeModeList) {
                                selectedPerson.add(treeMode.dataId);
                                stringBuilder.append(treeMode.dataId).append(",");
                            }
                            processModifyFormFieldDataMap.put(fullModel.formField.getId(),stringBuilder.toString());
                        }
                    });
                    dialog.show();
                }
            });
        }

        fullModel.addCustomControl(recyclerView);
        return fullModel.layout;
    }



    /**
     * 字段设置界面
     * @param context 上下文
     * @param installClass 配置
     * @return 1
     */
    @Override
    protected View getFieldInstallView(Context context, InstallClass installClass) {
        ObservableEmitter<ViewGroup> emitter = installClass.emitter;

        emitter.onNext(installClass.getIsMastView());        //是否必填
        emitter.onNext(installClass.getIsPrintView());        //是否参与打印

        return installClass.v;
    }
}
